package com.itheima.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.utils.SecurityUtils;
import com.itheima.common.utils.bean.BeanUtils;
import com.itheima.server.domain.Business;
import com.itheima.server.domain.BusinessCommunication;
import com.itheima.server.domain.BusinessDetail;
import com.itheima.server.dto.BusinessFollowDto;
import com.itheima.server.service.BusinessCommunicationService;
import com.itheima.server.service.BusinessDetailService;
import com.itheima.server.mapper.BusinessDetailMapper;
import com.itheima.server.service.IBusinessService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Kuon
* @description 针对表【hk_business_detail(商机详情表)】的数据库操作Service实现
* @createDate 2025-09-05 10:57:50
*/
@Service
@RequiredArgsConstructor
public class BusinessDetailServiceImpl extends ServiceImpl<BusinessDetailMapper, BusinessDetail>
    implements BusinessDetailService {
    private final BusinessCommunicationService businessCommunicationService;

    private final IBusinessService businessService;



    /**
     * 跟进
     * @param businessFollowDto
     * @return
     */
    @Override
    @Transactional
    public Boolean followBusiness(BusinessFollowDto businessFollowDto) {
        //1.添加或更新详细表
         //先查有无详情表，有更新，没有添加
        BusinessDetail businessDetail = new BusinessDetail();
        BeanUtils.copyProperties(businessFollowDto, businessDetail);
        businessDetail.setBusinessId(businessFollowDto.getBusinessId());
        businessDetail.setCreateTime(new Date());
        businessDetail.setUpdateTime(new Date());
        super.saveOrUpdate(businessDetail);



        //2.添加沟通表
        BusinessCommunication businessCommunication = new BusinessCommunication();
        BeanUtils.copyProperties(businessFollowDto, businessCommunication);
         //为key赋常量
        List<Integer> keyItemsArray = businessFollowDto.getKeyItemsArray();
        String keys= keyItemsArray.stream().map(keyItem -> keyItem.toString()).collect(Collectors.joining(","));
        businessCommunication.setKeys(keys);
        businessCommunication.setCreateTime(new Date());
        businessCommunication.setCreateBy(SecurityUtils.getUsername());


        boolean result = businessCommunicationService.save(businessCommunication);

        //3.修改商机表更进状态,性别，年龄，微信，QQ，地区
        Integer businessId = businessFollowDto.getBusinessId();
        Business business = businessService.getById(businessId);
        business.setStatus(businessFollowDto.getTrackStatus());
        business.setSex(businessFollowDto.getSex());
        business.setAge(businessFollowDto.getAge());
        business.setWeixin(businessFollowDto.getWeixin());
        business.setQq(businessFollowDto.getQq());
        business.setProvinces(businessFollowDto.getProvinces());
        business.setCity(businessFollowDto.getCity());

        business.setNextTime(businessFollowDto.getNextTime());


        business.setUpdateTime(new Date());
        businessService.updateById(business);

        return result;


    }
}




